package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.bpa.OtherContractsReplenishDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.bpa.dao.OtherContractsReplenishDao;
import top.infopub.mgr.bpa.dao.OtherContractsReviewDao;
import top.infopub.mgr.bpa.domain.OtherConReplenishDomain;
import top.infopub.mgr.bpa.service.OtherContractsReplenishService;
import top.infopub.mgr.bpa.service.OtherContractsReviewService;
import top.infopub.mgr.bpa.vo.OtherConReplenishVo;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.Basic;
import top.infopub.model.bpa.ExpenseInvoice;
import top.infopub.model.bpa.OtherContractsReplenish;
import top.infopub.model.bpa.ProjectAgencyPayment;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 其他合同补充协议 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-15
 */
@Service
public class OtherContractsReplenishServiceImpl implements OtherContractsReplenishService {

    @Autowired
    OtherContractsReplenishDao otherContractsReplenishDao;

    @Autowired
    OtherContractsReviewDao otherContractsReviewDao;

    @Autowired
    private BpaContractBpaReviewServiceImpl bpaReviewService;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Override
    public Page<OtherConReplenishDomain> selectByDomainWhere(Page<OtherConReplenishDomain> page, OtherConReplenishVo otherConReplenishVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(otherConReplenishVo.getDates())) {
            String[] splitTime = otherConReplenishVo.getDates().split("~");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            otherConReplenishVo.setStartDate(format.parse(splitTime[0]));
            otherConReplenishVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherConReplenishDomain>) otherContractsReplenishDao.selectByDomainWhere(otherConReplenishVo);
    }





    @Override
    public OtherContractsReplenishDto queryById(String id) {
        OtherContractsReplenishDto dto = new OtherContractsReplenishDto();
        //根据id查询主表数据
        OtherContractsReplenish otherContractsReplenish = otherContractsReplenishDao.findById(id);
        //根据主表数据的procode查询 项目基本信息
        String proCode = otherContractsReplenish.getProCode();
        if (!StringUtils.isEmpty(proCode)) {
            Basic basic = otherContractsReplenishDao.findByProCode(proCode);
            String projectLeader = otherContractsReplenish.getProjectLeader();
            if (!StringUtils.isEmpty(projectLeader)){
                basic.setProjectLeader(projectLeader);
            }
            dto.setBasic(basic);
        }
        //
        if (otherContractsReplenish != null) {
            dto.setOtherContractsReplenish(otherContractsReplenish);
        }
        //根据主表数据的id查询 项目代付费信息 类型 1合同评审页面  2补充协议页面
        if (otherContractsReplenish != null) {
            /*List<ProjectAgencyPayment> allList = otherContractsReviewDao.selectByOtherContracts("2");
            List<ProjectAgencyPayment> tree = buildTree(allList, id);
            dto.setProjectAgencyPaymentList(tree);*/
            //查询项目代付费信息
            List<ProjectAgencyPayment> list = otherContractsReviewDao.selectProjectAgencyPayment(id, "2");
            dto.setProjectAgencyPaymentList(list);

        }
        //根据主表数据的id查询 费用清单信息 类型 1合同评审页面  2补充协议页面
        if (otherContractsReplenish != null) {
            List<ExpenseInvoice> expenseInvoiceList = otherContractsReviewDao.selectInvoiceByOtherContractsId(id, "2");
            dto.setExpenseInvoiceList(expenseInvoiceList);
        }
        //附件信息
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(otherContractsReplenish.getProCode(), ProjectFileTypeEnum.FILE_TYPE107.getCode(), null);
        if (attachmentList.size() > 0 && attachmentList != null) {
            String[] fileAtts = bpaReviewService.fileAttaListToArray(attachmentList);
            dto.setFileAtt(fileAtts);
        }
        return dto;
    }

    /*项目下拉框*/
    @Override
    public List<Map<String, String>> projectDropDownBox() {
        return otherContractsReplenishDao.projectDropDownBox();
    }

    //根据procode查询项目基本信息
    @Override
    public Basic projectBasic(String proCode) {
        return otherContractsReviewDao.findByProCode(proCode);
    }


    @Override
    public int save(OtherContractsReplenishDto otherContractsReplenishDto, List<ProjectAttachment> attachmentList) {
        OtherContractsReplenish otherContractsReplenish = otherContractsReplenishDto.getOtherContractsReplenish();
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (otherContractsReplenish != null) {
            if (attachmentList != null && attachmentList.size() > 0) {
                //批量插入附件信息
                projectAttachmentDao.deleteByWhere(otherContractsReplenish.getProCode(), ProjectFileTypeEnum.FILE_TYPE107.getCode(), null);
                for (ProjectAttachment attachment : attachmentList) {
                    attachment.setProCode(otherContractsReplenish.getProCode());
                }
                projectAttachmentDao.insertBatch(attachmentList);
            }
            String id = otherContractsReplenish.getId();
            if (StringUtils.isEmpty(id)) {
                //第一次 保存
                otherContractsReplenish.setAddBy(vo.getId());
                otherContractsReplenish.setAddTime(new Date());
                otherContractsReplenish.setProcessStatus("0");
                //保存主表数据
                otherContractsReplenishDao.insertOtherContractsReplenish(otherContractsReplenish);
                //保存费用清单信息表
                List<ExpenseInvoice> expenseInvoiceList = otherContractsReplenishDto.getExpenseInvoiceList();
                for (ExpenseInvoice expenseInvoice : expenseInvoiceList) {
                    expenseInvoice.setType("2");
                    expenseInvoice.setAddTime(new Date());
                    otherContractsReviewDao.insertExpenseInvoice(expenseInvoice);
                }
                //List<ProjectAgencyPayment> projectAgencyPaymentList = otherContractsReplenishDto.getProjectAgencyPaymentList();
                //OtherContractsReviewServiceImpl.saveAndUpdate(projectAgencyPaymentList,otherContractsReplenish.getId(),"2");
            } else {
                //修改
                otherContractsReplenish.setUptBy(vo.getId());
                otherContractsReplenish.setUptTime(new Date());
                otherContractsReplenishDao.updateOtherContractsReplenish(otherContractsReplenish);
                //保存费用清单信息表
                List<ExpenseInvoice> expenseInvoiceList = otherContractsReplenishDto.getExpenseInvoiceList();
                for (ExpenseInvoice expenseInvoice : expenseInvoiceList) {
                    if (StringUtils.isEmpty(expenseInvoice.getId())) {
                        //保存
                        expenseInvoice.setType("2");
                        expenseInvoice.setAddTime(new Date());
                        expenseInvoice.setMasterId(otherContractsReplenish.getId());
                        otherContractsReviewDao.insertExpenseInvoice(expenseInvoice);
                    } else {
                        //修改
                        expenseInvoice.setType("2");
                        otherContractsReviewDao.updateExpenseInvoice(expenseInvoice);
                    }
                }
                List<ProjectAgencyPayment> projectAgencyPaymentList = otherContractsReplenishDto.getProjectAgencyPaymentList();
                //OtherContractsReviewServiceImpl.saveAndUpdate(projectAgencyPaymentList,id,"2");
                //删除合同清单录入数据
                otherContractsReviewDao.deleteProjectAgencyPayment(id,"2");
                //保存合同清单录入数据
                int seq2 = 0;
                for (ProjectAgencyPayment payment : projectAgencyPaymentList) {
                    payment.setType("2");
                    payment.setContractId(id);
                    payment.setSeq(seq2++);
                }
                if (projectAgencyPaymentList!=null && projectAgencyPaymentList.size()>0){
                    otherContractsReviewDao.insertProjectAgencyPayment(projectAgencyPaymentList);
                }
            }
        }
        return 1;
    }

    @Override
    public OtherContractsReplenish findById(String id) {
        return otherContractsReplenishDao.findById(id);
    }


    //查询树的公共方法
    /*    public static List<ProjectAgencyPayment> buildTree(List<ProjectAgencyPayment> treeList, String pId) {
            List<ProjectAgencyPayment> childrenList = new ArrayList<ProjectAgencyPayment>();
            List<ProjectAgencyPayment> successorList = new ArrayList<ProjectAgencyPayment>();
            for (ProjectAgencyPayment tree : treeList) {
                if (pId.equals(tree.getMasterId())) {
                    childrenList.add(tree);
                } else {
                    successorList.add(tree);
                }
            }
            for (ProjectAgencyPayment tree : childrenList) {
                List<ProjectAgencyPayment> trees = buildTree(successorList, tree.getId());
                tree.setProjectAgencyPaymentList(new ArrayList<ProjectAgencyPayment>());
                for (ProjectAgencyPayment tree1 : trees) {
                    tree.getProjectAgencyPaymentList().add(tree1);
                }
            }
            return childrenList;
        }*/



}
